﻿namespace CookieTerm
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public partial class MainForm : Form
    {
        BookmarkManager bookmarkManager;

        public MainForm()
        {
            InitializeComponent();

            this.bookmarkManager = new BookmarkManager();
            this.bookmarkManager.Load();

            LoadBookmarkMenu();
        }

        void LoadBookmarkMenu()
        {
            this.openToolStripMenuItem.DropDownItems.Clear();

            for (int i = 0; i < this.bookmarkManager.Bookmarks.Count; i++)
            {
                string text = (i < 10 ? "&" : "") + i + " " + this.bookmarkManager.Bookmarks[i].Name;
                ToolStripMenuItem item = new ToolStripMenuItem(text);
                item.Name = this.bookmarkManager.Bookmarks[i].Name;
                if (i < 10)
                {
                    item.ShortcutKeys = Keys.Control | Keys.Alt | (Keys.D0 + i);
                }
                item.Click += new EventHandler(BookmarkClick);
                this.openToolStripMenuItem.DropDownItems.Add(item);
            }
        }

        void BookmarkClick(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            BbsForm newPage = new BbsForm(this.bookmarkManager.FindBookmark(menu.Name));
            newPage.OnStatusChanged += bbsForm_OnStatusChanged;
            newPage.MdiParent = this;
            newPage.Show();
        }

        void bbsForm_OnStatusChanged(object sender, EventArgs args)
        {
            if (sender == ActiveMdiChild)
            {
                this.connectionStatusLabel.Text = (ActiveMdiChild as BbsForm).StatusText;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if ((int)keyData == (int)Keys.F4 + (int)Keys.Control && ActiveMdiChild != null)
            {
                ActiveMdiChild.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnMdiChildActivate(EventArgs e)
        {
            base.OnMdiChildActivate(e);
            BbsForm activePage = (BbsForm)ActiveMdiChild;

            if (activePage != null)
            {
                this.connectionStatusLabel.Text = activePage.StatusText;
            }
            else
            {
                this.connectionStatusLabel.Text = string.Empty;
            }
        }

        private void manageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BookmarkEditor bookmarkEditor = new BookmarkEditor(this.bookmarkManager);
            bookmarkEditor.ShowDialog();
            if (bookmarkEditor.changed)
            {
                LoadBookmarkMenu();
                foreach (Form page in MdiChildren)
                {
                    ((BbsForm)page).Console.RefreshDisplaySettings();
                }
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BbsForm activeForm = (BbsForm)ActiveMdiChild;

            if (activeForm != null)
            {
                activeForm.Console.Copy();
            }
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BbsForm activeForm = (BbsForm)ActiveMdiChild;

            if (activeForm != null)
            {
                activeForm.Console.Paste();
            }
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BbsForm activeForm = (BbsForm)ActiveMdiChild;

            if (activeForm != null)
            {
                activeForm.Console.SelectAll();
            }
        }

        private void launchUrlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            BbsForm activeForm = (BbsForm)ActiveMdiChild;

            if (activeForm != null)
            {
                activeForm.Console.LaunchSelectionAsUrl();
            }
        }
    }
}
